﻿#include <QSettings>
#include <QTextCodec>
#include <QStandardPaths>

#include "QtMapWidget/QtMapWidget.h"
#include "TiandituMapProvider.h"

/**************************************
 *
 * class TiandituMapProvider
 *
 **************************************/
TiandituMapProvider::TiandituMapProvider(const QString &format, const quint32 averageSize,
                                         const QGeoMapType::MapStyle mapType, QObject *parent)
    : MapProvider(QStringLiteral("https://vgimap.tianditu.gov.cn/"), format, averageSize, mapType, parent)
{
    QString writableLocation = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
    QString file = QString("%1/%2.ini").arg(writableLocation, WidgetName);
    QSettings* settings = new QSettings(file, QSettings::IniFormat);
    _token = settings->value("Map/TiandituAccessToken", "").value<QString>();
    if (_token.isEmpty()) {
        _token = "a8341d81e4fd52009981dffe1e77943a";
        settings->setValue("Map/TiandituAccessToken", _token);
        settings->sync();
    }
    _sld = settings->value("Map/TiandituSLD", "").value<QString>();
    if (_sld.isEmpty()) {
        _sld = "t0";
        settings->setValue("Map/TiandituSLD", _sld);
        settings->sync();
    }
}

/**
 * @brief 因为使用父类提供的 virtual 版本会导致 server replied: Forbidden，
 *        所以在子类中作了 override 实现，主要区别是 "Accept" 字段
 * @param x
 * @param y
 * @param zoom
 * @param networkManager
 * @return
 */
QNetworkRequest TiandituMapProvider::getTileURL(const int x, const int y, const int zoom, QNetworkAccessManager *networkManager)
{
    //-- Build URL
    QNetworkRequest request;
    const QString url = _getURL(x, y, zoom, networkManager);
    if (url.isEmpty()) {
        return request;
    }
    request.setUrl(QUrl(url));
    request.setRawHeader(QByteArrayLiteral("Accept"), QByteArrayLiteral("text/html,image/*"));
    request.setRawHeader(QByteArrayLiteral("Referrer"), _referrer.toUtf8());
    request.setRawHeader(QByteArrayLiteral("User-Agent"), _userAgent);
    return request;
}

/**************************************
 *
 * class TiandituImgMapProvider
 *
 **************************************/

TiandituImgMapProvider::TiandituImgMapProvider(QObject *parent)
    : TiandituMapProvider("png", AVERAGE_TILE_SIZE, QGeoMapType::SatelliteMapDay, parent)
{
}

QString TiandituImgMapProvider::_getURL(const int x, const int y, const int zoom, QNetworkAccessManager *networkManager)
{
    Q_UNUSED(networkManager)
    const QString xx = QString("%1").arg(x);
    const QString yy = QString("%1").arg(y);
    const QString zz = QString("%1").arg(zoom);
    return tiandituImg.arg(_sld, zz, yy, xx, _token);
}

/**************************************
 *
 * class TiandituCiaMapProvider
 *
 **************************************/

TiandituCiaMapProvider::TiandituCiaMapProvider(QObject *parent)
    : TiandituMapProvider("png", AVERAGE_TILE_SIZE, QGeoMapType::StreetMap, parent)
{
}

QString TiandituCiaMapProvider::_getURL(const int x, const int y, const int zoom, QNetworkAccessManager *networkManager)
{
    Q_UNUSED(networkManager)
    const QString xx = QString("%1").arg(x);
    const QString yy = QString("%1").arg(y);
    const QString zz = QString("%1").arg(zoom);
    return tiandituCia.arg(_sld, zz, yy, xx, _token);
}

/**************************************
 *
 * class TiandituVecMapProvider
 *
 **************************************/

TiandituVecMapProvider::TiandituVecMapProvider(QObject *parent)
    : TiandituMapProvider("png", AVERAGE_TILE_SIZE, QGeoMapType::StreetMap, parent)
{
}

QString TiandituVecMapProvider::_getURL(const int x, const int y, const int zoom, QNetworkAccessManager *networkManager)
{
    Q_UNUSED(networkManager)
    const QString xx = QString("%1").arg(x);
    const QString yy = QString("%1").arg(y);
    const QString zz = QString("%1").arg(zoom);
    return tiandituVec.arg(_sld, zz, yy, xx, _token);
}

/**************************************
 *
 * class TiandituCvaMapProvider
 *
 **************************************/

TiandituCvaMapProvider::TiandituCvaMapProvider(QObject *parent)
    : TiandituMapProvider("png", AVERAGE_TILE_SIZE, QGeoMapType::StreetMap, parent)
{
}

QString TiandituCvaMapProvider::_getURL(const int x, const int y, const int zoom, QNetworkAccessManager *networkManager)
{
    Q_UNUSED(networkManager)
    const QString xx = QString("%1").arg(x);
    const QString yy = QString("%1").arg(y);
    const QString zz = QString("%1").arg(zoom);
    return tiandituCva.arg(_sld, zz, yy, xx, _token);
}
